package com.tianzeculture.stagebao.activity.artist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.adapter.AddPriceAdapter;
import com.tianzeculture.stagebao.activity.home.MainActivity;
import com.tianzeculture.stagebao.activity.personal.SelectJobActivity;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.News;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.net.ArtistRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.DensityUtil;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;
import com.tianzeculture.stagebao.view.MyRecyclerview;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/28 0028.
 * 加入人才库-填写个人信息
 */

public class AddTalentDataActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;

//    @BindView(R.id.layoutWorks)
//    LinearLayout layoutWorks;

    @BindView(R.id.tvHeight)
    TextView tvHeight;
    @BindView(R.id.tvWeight)
    TextView tvWeight;
    //    @BindView(R.id.tvBust)
//    TextView tvBust;
//    @BindView(R.id.tvWaist)
//    TextView tvWaist;
//    @BindView(R.id.tvHipline)
//    TextView tvHipline;
    @BindView(R.id.tvShoeSize)
    TextView tvShoeSize;

//    @BindView(R.id.tvEditPersonWork)
//    TextView tvEditPersonWork;
    @BindView(R.id.tvEditArtistData)
    TextView tvEditArtistData;
    @BindView(R.id.tvEditPrice)
    TextView tvEditPrice;
    @BindView(R.id.tvEditPersonTag)
    TextView tvEditPersonTag;
    @BindView(R.id.tvEditWorkExperience)
    TextView tvEditWorkExperience;
    @BindView(R.id.tv_experience)
    TextView tvExperience;
    @BindView(R.id.myRecyclerview)
    MyRecyclerview myRecyclerview;
    @BindView(R.id.tv_style)
    TextView tvStyle;
    @BindView(R.id.tv_outward)
    TextView tvOutward;
    @BindView(R.id.tv_bodytype)
    TextView tvBodytype;
    @BindView(R.id.tv_charm)
    TextView tvCharm;

    @BindView(R.id.btnSave)
    Button btnSave;

    List<String> imgs = null;

    User myUser = null;

    String style;
    String outward;
    String bodytype;
    String charm;

    String userHeight;
    String userWeight;
    String shoeSize;

    String experience;
    @Request
    ArtistRestUsage artistRestUsage = null;
    private int addTalentPool = 105;

    ArrayList<String> stringlist = new ArrayList<>();
    AddPriceAdapter adapter;

    @Override
    public void initUI() {
        super.initUI();
        EventBus.getDefault().register(this);
        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);
        tvTitle.setText(getString(R.string.add_talent));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myRecyclerview.setLayoutManager(linearLayoutManager);
        adapter = new AddPriceAdapter(R.layout.item_add_price, stringlist);
        myRecyclerview.setAdapter(adapter);
//        updatePersonWork();
    }

    private static final int REQUEST_IMAGE = 1001;

    @OnClick({ R.id.tvEditArtistData, R.id.tvEditPrice,
            R.id.tvEditPersonTag, R.id.tvEditWorkExperience, R.id.btnSave})
    public void onclick(View view) {
        switch (view.getId()) {
//            case R.id.tvEditPersonWork://个人代表作
////                List<String> strings = new ArrayList<>();
////                strings.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4053404310,3325634318&fm=27&gp=0.jpg");
////                strings.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3381146714,2496287773&fm=27&gp=0.jpg");
////                strings.add("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=843317495,456431923&fm=27&gp=0.jpg");
////                strings.add("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=572240932,2008738278&fm=27&gp=0.jpg");
//                Intent intent = new Intent(AddTalentDataActivity.this, ArtistEditPersonWorkActivity.class);
//                intent.putStringArrayListExtra("imgs", (ArrayList<String>) imgs);
//                startActivityForResult(intent, Util.TO_ADD_PHOTO);
//
//                break;
            case R.id.tvEditArtistData://艺人资料
                Intent intent1 = new Intent(AddTalentDataActivity.this, ArtistEditDataActivity.class);
                myUser = Util.user;
                intent1.putExtra("user", myUser);
                startActivityForResult(intent1, Util.EDIT_ARTIST_BASIC);
                break;
            case R.id.tvEditPrice://报价
                Intent intent2 = new Intent(this, SelectJobActivity.class);
                intent2.putExtra("isAdd", true);
//                intent2.putExtra("price",)
                startActivityForResult(intent2, Util.EDIT_ARTIST_PRICE);
                break;
            case R.id.tvEditPersonTag://个人标签
                Intent intent3 = new Intent(AddTalentDataActivity.this, ArtistEditTagActivity.class);
                startActivityForResult(intent3, Util.HOT_TAG);
                break;
            case R.id.tvEditWorkExperience://工作经验
                Intent intent4 = new Intent(this, ExperienceActivity.class);
                startActivityForResult(intent4, Util.WORK_EXPERIENCE);
                break;
            case R.id.btnSave:
                String money = null;
                if (stringlist.size() > 0) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < stringlist.size(); i++) {
                        if (i == stringlist.size() - 1) {
                            sb.append(stringlist.get(i));
                        }
                        sb.append(stringlist.get(i));
                        sb.append(",");
                    }
                    money = sb.toString();
                }
                Map<String, String> params = new HashMap<>();
                params.put("userHeight", userHeight);
                params.put("userWeight", userWeight);
                params.put("shoeSize", shoeSize);
                params.put("tag", money);
                params.put("tag1", style);
                params.put("tag2", outward);
                params.put("tag3", bodytype);
                params.put("tag4", charm);
                params.put("experience", experience);
                params.put("alent", "1");
                artistRestUsage.addTalentPool(addTalentPool, params);
                break;
        }
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == addTalentPool) {
            if (msg.getData()) {
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_add_talent_data;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Util.TO_ADD_PHOTO) {//个人代表作
            imgs = (data == null ? null : data.getStringArrayListExtra("imgs"));
//            updatePersonWork();
        } else if (resultCode == Util.EDIT_ARTIST_BASIC) {//艺人资料
            if (data == null)
                return;
            myUser = (User) data.getSerializableExtra("user");
            updateBasic();
        }

//        if (requestCode == Util.EDIT_ARTIST_PRICE) {
//            if (data == null)
//                return;
//            String str = data.getExtras().getString("atistPrice");
//            stringlist.add(str);
//            adapter.notifyDataSetChanged();
//
//        }
        if (requestCode == Util.HOT_TAG) {
            if (data == null)
                return;
            style = data.getExtras().getString("style");
            outward = data.getExtras().getString("outward");
            bodytype = data.getExtras().getString("bodytype");
            charm = data.getExtras().getString("charm");
            tvStyle.setText(style);
            tvOutward.setText(outward);
            tvBodytype.setText(bodytype);
            tvCharm.setText(charm);
        }
        if (requestCode == Util.WORK_EXPERIENCE) {
            if (data == null)
                return;
            experience = data.getExtras().getString("experience");
            tvExperience.setVisibility(View.VISIBLE);
            tvExperience.setText(experience);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getPrice(News news){
        stringlist.add(news.getNewcontent());
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //    /**
//     * 更新个人代表作
//     */
//    private void updatePersonWork() {
//        if (imgs == null)
//            imgs = new ArrayList<>();
//        for (int i = 0; i < imgs.size(); i++) {
//            View view = LayoutInflater.from(AddTalentDataActivity.this).inflate(R.layout.item_artist_data_photo, null);
//            ImageView imageView = view.findViewById(R.id.ivPhoto);
//            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
//            layoutParams.width = DensityUtil.dp2px(AddTalentDataActivity.this, 264);
//            layoutParams.height = DensityUtil.dp2px(AddTalentDataActivity.this, 343);
//            imageView.setLayoutParams(layoutParams);
//            Glide.with(AddTalentDataActivity.this).load(imgs.get(i)).into(imageView);
//            layoutWorks.addView(view);
//        }
//    }

    /**
     * 更新艺人资料
     */
    private void updateBasic() {
        if (myUser == null) {
            myUser = new User();
        }
        userHeight = myUser.getUserHeight();
        userWeight = myUser.getUserWeight();
//        userBust = myUser.getUserBust();
//        userWaist = myUser.getUserWaist();
//        userHipline = myUser.getUserHipline();
        shoeSize = myUser.getShoeSize();
        tvHeight.setText("身高:" + userHeight + "cm");
        tvWeight.setText("体重:" + userWeight + "kg");
//        tvBust.setText("胸围:" + userBust + "cm");
//        tvWaist.setText("腰围:" + userWaist + "cm");
//        tvHipline.setText("臀围:" + userHipline + "cm");
        tvShoeSize.setText("鞋码:" + shoeSize + "码");
    }
}